<?php

require dirname(dirname(__FILE__)) . '/includes/initialize.php';

require dirname(dirname(__FILE__)) . '/includes/common_head.php';
if (file_exists(str_replace('//', '/', $app_root . $site['home_dir'] . '/admin-custom/common_head.php'))) {
	require str_replace('//', '/', $app_root . $site['home_dir'] . '/admin-custom/common_head.php');
}

if ($site['auth_user']) {
	
	# Workaround as PHP in CGI mode doesn't get the basic auth details.
	if(isset($_SERVER['HTTP_AUTHORIZATION'])) {
		$auth_params = explode(":", base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)), 2);
		$_SERVER['PHP_AUTH_USER'] = $auth_params[0];
		$_SERVER['PHP_AUTH_PW']   = $auth_params[1];
	}
	
	if (!isset($_SERVER['PHP_AUTH_USER']) or !($_SERVER['PHP_AUTH_USER'] == $site['auth_user'] and $_SERVER['PHP_AUTH_PW'] == $site['auth_pass'])) {
		header('WWW-Authenticate: Basic realm="' . $site['domain_name'] . '"');
		header('HTTP/1.0 401 Unauthorized');
		echo "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n";
		echo "<html><head>\n";
		echo "<title>Forbidden</title>\n";
		echo "</head><body>\n";
		echo "<h1>Forbidden</h1>\n";
		echo "<p>You don't have permission to access {$_SERVER['REQUEST_URI']} on this server.</p>\n";
		echo "<hr>\n";
		echo "<address>".$_SERVER['SERVER_SOFTWARE']." at ".$_SERVER['SERVER_NAME']."</address>\n";
		echo "<pre>";
		echo "</body></html>\n";
		exit;
	}
	
}

$smarty->template_dir = $app_root . $site['home_dir'] . '/templates';
$smarty->compile_id   = $site['id'] . 'frontend';

$smarty->assign('resources', "{$site['home_dir']}/resources");
require 'modifier.var.php';
require 'modifier.index.php';
require 'filter.cdn.php';


for($i=count($path);$i>0;$i--) {
	if (file_exists(str_replace('//', '/', $app_root . $site['home_dir'] . '/admin-custom/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i)) . '/index.php')))) {
		$logger->debug(str_replace('//', '/', $app_root . $site['home_dir'] . '/admin-custom/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i)) . '/index.php')));
		$handler = str_replace('//', '/', $app_root . $site['home_dir'] . '/admin-custom/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i)) . '/index.php'));
		break;
	} elseif (file_exists(str_replace('//', '/', $app_root . $site['home_dir'] . '/admin-custom/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i-1))) . '/' . $path[$i-1] . '.php'))) {
		$logger->debug(str_replace('//', '/', $app_root . $site['home_dir'] . '/admin-custom/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i-1))) . '/' . $path[$i-1] . '.php'));
		$handler = str_replace('//', '/', $app_root . $site['home_dir'] . '/admin-custom/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i-1))) . '/' . $path[$i-1] . '.php');
		break;
	} elseif (file_exists(str_replace('//', '/', $app_root . '/admin/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i)) . '/index.php')))) {
		$logger->debug(str_replace('//', '/', $app_root . '/admin/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i)) . '/index.php')));
		$handler = str_replace('//', '/', $app_root . '/admin/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i)) . '/index.php'));
		break;
	} elseif (file_exists(str_replace('//', '/', $app_root . '/admin/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i-1))) . '/' . $path[$i-1] . '.php'))) {
		$logger->debug(str_replace('//', '/', $app_root . '/admin/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i-1))) . '/' . $path[$i-1] . '.php'));
		$handler = str_replace('//', '/', $app_root . '/admin/frontend/' . escapeshellcmd(join('/', array_slice($path, 0, $i-1))) . '/' . $path[$i-1] . '.php');
		break;
	}
}

if (isset($handler)) {
	require $handler;
} else {
	if (file_exists($app_root . $site['home_dir'] . '/admin-custom/frontend/fallback.php')) {
		require $app_root . $site['home_dir'] . '/admin-custom/frontend/fallback.php';
	} else {
		require $app_root . '/admin/frontend/fallback.php';
	}
}

require dirname(dirname(__FILE__)) . '/includes/common_foot.php';
